#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    // 请在此输入您的代码
    int N, M;
    cin >> N >> M;
    vector<int> a(N + 1), b(M + 1);
    for (int i = 1;i <= N;i++)
    {
        cin >> a[i];
    }
    for (int i = 1;i <= M;i++)
    {
        cin >> b[i];
    }

    //一定要注意边界，比如这里面的dp[i][j]，i和j是表示前多少个字母，因此i和j的边界应该要到达N和M
    vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
    for (int i = 1;i <= N;i++)
    {
        for (int j = 1;j <= M;j++)
        {
            if (a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
    cout << dp[N][M] << endl;
    return 0;
}